package com.xm.zcf.action.order;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.xm.zcf.domain.adminuser.Adminuser;
import com.xm.zcf.domain.cart.CarItem;
import com.xm.zcf.domain.cart.Cart;
import com.xm.zcf.domain.order.Orderitem;
import com.xm.zcf.domain.order.Orders;
import com.xm.zcf.domain.product.Product;
import com.xm.zcf.service.order.OrderService;
import com.xm.zcf.util.PageBean;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by Administrator on 2017/4/7.
 */
@Controller
@Scope("prototype")
public class OrderAction extends ActionSupport implements ModelDriven<Orders>{
    private Orders model=new Orders();
    private Integer page;
    private Integer limit=4;

    @Resource
    private OrderService orderService;


    public Integer getPage() {
        return page;
    }

    public void setPage(Integer page) {
        this.page = page;
    }

    public Integer getLimit() {
        return limit;
    }

    public void setLimit(Integer limit) {
        this.limit = limit;
    }

    public String addOrder(){
        //判断是否登录
        Adminuser adminuser=(Adminuser) ActionContext.getContext().getSession().get("existUser");
        if(adminuser==null){
            return "login";
        }
        //判断购物车是否添加
        Cart cart=(Cart)ActionContext.getContext().getSession().get("cart");
        if(cart==null){
            this.addActionMessage("购物车是空的！");
            return "msg";
        }
        //补全订单信息
        model.setState(1);
        model.setAdminuser(adminuser);
        model.setTotal(cart.getTotal());
        model.setOrdertime(getTime());
        model.setName(adminuser.getName());
        model.setPhone(adminuser.getPhone());
        model.setAddr(adminuser.getAddr());

        //从购物车取出条目，补全订单条目
        for (CarItem cartitem:cart.getCartItems()){
            Orderitem item =new Orderitem();//每取出一个，生成一个订单
            item.setCount(cartitem.getCount());//商品个数
            item.setOsatate(1);//不能评价
            item.setSubtotal(cartitem.getSubTotal());//小计
            model.getOrderitem().add(item);
        }
        /*保存订单*/
        orderService.add(model);
        cart.clearCart();
        return "order";
    }
    @Override
    public Orders getModel() {
        return model;
    }

    public String findByUid(){
        Adminuser adminuser=(Adminuser) ActionContext.getContext().getSession().get("existUser");
        PageBean list=orderService.findByUid(page,limit,adminuser.getUid());
        ActionContext.getContext().getValueStack().push(list);
        return "orderList";
    }
    public String findByOid(){
        Orders orders=orderService.findByOid(model.getOid());
        ActionContext.getContext().getValueStack().push(orders);
        return "order";
    }
    public String payOrder(){
        Orders orders=orderService.findByOid(model.getOid());
        orders.setName(model.getName());
        orders.setAddr(model.getAddr());
        orders.setPhone(model.getPhone());
        orders.setState(2);
        orderService.update(orders);
        this.addActionMessage("订单编号"+orders.getOid()+",付款成功！");
        return "msg";
    }
    public String finshOrder(){
        Orders orders=orderService.findByOid(model.getOid());
        orders.setState(4);
        for(Orderitem orderitem:orders.getOrderitem()){
            orderitem.setOsatate(2);
        }
        orderService.update(orders);
        return "tolist";
    }
    public String getTime() {
        SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        return sf.format(new Date());
    }
}
